package com.msb.c02;

/**
 * @author: xw
 * @create: 2022/9/22 11:04
 * @description: 同步锁
 */
public class SynchronizedTest01 {

    private /*volatile*/ int count = 10;
    private Object lock = new Object();
    public void m() {
        synchronized(lock) {
            count--;
            System.out.println(Thread.currentThread().getName() + " count = " + count);
        }
    }
    public void m1() {
        synchronized(this) {
            count--;
            System.out.println(Thread.currentThread().getName() + " count = " + count);
        }
    }
    public static void main(String[] args) {
        SynchronizedTest01 synchronizedTest01 = new SynchronizedTest01();
            new Thread(()->{
                synchronizedTest01.m();
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }).start();

            new Thread(()->{
                synchronizedTest01.m1();
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }).start();


    }
}
